package cn.yhjz.generator.controller;

import cn.yhjz.common.AjaxResponse;
import cn.yhjz.common.util.IdUtil;
import cn.yhjz.platform.system.util.LoginUtil;
import cn.yhjz.generator.entity.SysGenModel;
import cn.yhjz.generator.service.ISysGenModelService;
import cn.yhjz.generator.service.ISysGenTemplateService;
import cn.yhjz.generator.entity.SysGenTemplate;
import cn.yhjz.generator.util.TemplateUtil;
import cn.yhjz.generator.vo.SysGenModelVo;
import cn.yhjz.generator.vo.SysGenTemplateVo;

import freemarker.template.TemplateException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Date;


@RestController
@RequestMapping("/sysgen/template")
public class SysGenTemplateController {

    @Autowired
    private ISysGenTemplateService sysGenTemplateService;
    @Autowired
    private ISysGenModelService modelService;
    @Autowired
    private TemplateUtil templateUtil;

    @PostMapping("/list")
    public AjaxResponse list(@RequestBody SysGenTemplate sysGenTemplate) {
        return AjaxResponse.success(sysGenTemplateService.select(sysGenTemplate));
    }

    @PostMapping("/page")
    public AjaxResponse page(@RequestBody SysGenTemplateVo vo) {
        SysGenTemplate entity = new SysGenTemplate();
        BeanUtils.copyProperties(vo, entity);
        return AjaxResponse.success(sysGenTemplateService.getPage(vo.getCurrentPage(), vo.getPageSize(), entity));
    }

    @PostMapping("/addOrUpdate")
    public AjaxResponse add(@RequestBody SysGenTemplate entity) {
        if (entity.getId() != null) {
            entity.setUpdatedBy(LoginUtil.getCurrentUser().getUserCode());
            entity.setUpdatedTime(new Date());
            return AjaxResponse.success(sysGenTemplateService.updateById(entity));
        } else {
            entity.setTemplateCode(IdUtil.generate());
            entity.setCreatedTime(new Date());
            entity.setCreatedBy(LoginUtil.getCurrentUser().getUserCode());
            return AjaxResponse.success(sysGenTemplateService.insert(entity));
        }
    }

    @GetMapping("/selectById")
    public AjaxResponse selectById(Long id) {
        return AjaxResponse.success(sysGenTemplateService.selectById(id));
    }

    @DeleteMapping("/deleteById")
    public AjaxResponse deleteById(Long id) {
        return AjaxResponse.success(sysGenTemplateService.deleteById(id));
    }

    @PostMapping("/previewCode")
    public AjaxResponse previewCode(@RequestBody SysGenTemplateVo vo) throws TemplateException, IOException {
        //
        SysGenModelVo modelVo = modelService.selectById(vo.getModelId());
        SysGenModel model = new SysGenModel();
        BeanUtils.copyProperties(modelVo, model);
        SysGenTemplate template = new SysGenTemplate();
        template.setTemplate(vo.getTemplate());
        String code = templateUtil.assemble(model, template);
        return AjaxResponse.success("success", code);
    }
}
